package me.zhengjie.modules.houseshow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.modules.company.domain.Company;
import me.zhengjie.modules.company.service.mapper.CompanyMapper;
import me.zhengjie.modules.houseshow.domain.vo.*;
import me.zhengjie.modules.houseshowconfig.domain.HouseShowConfig;
import me.zhengjie.modules.houseshowconfig.service.mapper.HouseShowConfigMapper;
import me.zhengjie.modules.houseshowscene.domain.HouseShowScene;
import me.zhengjie.modules.houseshowscene.service.mapper.HouseShowSceneMapper;
import me.zhengjie.modules.houseshowscenehot.domain.HouseShowSceneHot;
import me.zhengjie.modules.houseshowscenehot.service.mapper.HouseShowSceneHotMapper;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.houseshow.domain.HouseShow;
import me.zhengjie.modules.houseshow.service.HouseShowService;
import me.zhengjie.modules.houseshow.service.dto.HouseShowDto;
import me.zhengjie.modules.houseshow.service.dto.HouseShowQueryParam;
import me.zhengjie.modules.houseshow.service.mapper.HouseShowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author TianI
* @date 2021-12-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = HouseShowService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HouseShowServiceImpl extends CommonServiceImpl<HouseShowMapper, HouseShow> implements HouseShowService {

    // private final RedisUtils redisUtils;
    private final HouseShowMapper houseShowMapper;

    private final HouseShowConfigMapper houseShowConfigMapper;

    private final HouseShowSceneMapper houseShowSceneMapper;
    private final CompanyMapper CompanyMapper;
    private final HouseShowSceneHotMapper hotMapper;

    @Override
    public PageInfo<HouseShowDto> queryAll(HouseShowQueryParam query, Pageable pageable) {
        IPage<HouseShow> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<HouseShow> page = houseShowMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, HouseShowDto.class);
    }

    @Override
    public List<HouseShowDto> queryAll(HouseShowQueryParam query) {
        return ConvertUtil.convertList(houseShowMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), HouseShowDto.class);
    }

    @Override
    public HouseShow getById(Long id) {
        return houseShowMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public HouseShowDto findById(Long id) {
        return ConvertUtil.convert(getById(id), HouseShowDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HouseShowDto resources) {
        HouseShow entity = ConvertUtil.convert(resources, HouseShow.class);
        return houseShowMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(HouseShowDto resources) {
        HouseShow entity = ConvertUtil.convert(resources, HouseShow.class);
        int ret = houseShowMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids) {
        // delCaches(ids);
        return houseShowMapper.deleteBatchIds(ids);
    }

    @Override
    public GlobalVo getGlobalById(String hsId) {
        GlobalVo globalVo = new GlobalVo();
        HouseShow houseShow = houseShowMapper.selectById(hsId);
        String title = houseShow.getTitle();
        Long companyId = houseShow.getCompanyId();
        globalVo.setTitle(title);
        Company company = CompanyMapper.selectById(companyId);
        globalVo.setCompany(company);
        QueryWrapper<HouseShowConfig> wrapper = new QueryWrapper<HouseShowConfig>();
        wrapper.eq("house_show_id", hsId);
        List<HouseShowConfig> houseShowConfigs = houseShowConfigMapper.selectList(wrapper);
        List<HouseTypeVo> typeList = new ArrayList<>();

        for (HouseShowConfig showConfig : houseShowConfigs) {
            String type = showConfig.getType();
            String name = showConfig.getName();
            HouseTypeVo houseTypeVo = new HouseTypeVo();
            houseTypeVo.setHouseTypeId(type);
            houseTypeVo.setHouseTypeName(name);
            typeList.add(houseTypeVo);
        }
        globalVo.setHouseType(typeList);
        return globalVo;
    }

    @Override
    public PanoramaVo getPanoramaById(String hsId) {
        PanoramaVo panoramaVo = new PanoramaVo();
        panoramaVo.setMenuShow(true);
        HouseShowConfig HouseShowConfig = houseShowConfigMapper.selectByHouseShowId(hsId);
        String previewPanorama = HouseShowConfig.getPreviewPanorama();
        String detail = HouseShowConfig.getDetail();
        panoramaVo.setPreviewPanorama(previewPanorama);
        panoramaVo.setDetail(detail);
        QueryWrapper<HouseShowScene> wrapper = new QueryWrapper<HouseShowScene>();
        wrapper.eq("house_show_config_id", HouseShowConfig.getHouseShowConfigId());
        List<HouseShowScene> houseShowScenes = houseShowSceneMapper.selectList(wrapper);
        for (HouseShowScene houseShowScene : houseShowScenes) {
            String houseShowSceneId = houseShowScene.getHouseShowSceneId();
            QueryWrapper<HouseShowSceneHot> hotWrapper = new QueryWrapper<HouseShowSceneHot>();
            hotWrapper.eq("house_show_scene_id", houseShowSceneId);
            List<HouseShowSceneHot> houseShowSceneHots = hotMapper.selectList(hotWrapper);
            houseShowScene.setHotSpots(houseShowSceneHots);
            houseShowScene.setHotSpotDebug(false);
            ValueVo valueVo = new ValueVo();
            valueVo.setHfov(houseShowScene.getHfov());
            valueVo.setPitch(houseShowScene.getPitch());
            valueVo.setYaw(houseShowScene.getYaw());
            valueVo.setType(houseShowScene.getType());
            valueVo.setCubeMap(houseShowScene.getCubeMap());
            valueVo.setHotSpots(houseShowScene.getHotSpots());
            valueVo.setHotSpotDebug(false);
            houseShowScene.setValueVo(valueVo);

        }
        List<PanoramaSceneVo> sceneVo = new ArrayList<>();
        for (HouseShowScene houseShowScene : houseShowScenes) {
            String houseShowSceneId = houseShowScene.getHouseShowSceneId();
            String text = houseShowScene.getText();
            String img = houseShowScene.getImg();
            PanoramaSceneVo panoramaSceneVo = new PanoramaSceneVo();
            panoramaSceneVo.setHouseShowSceneId(houseShowSceneId);
            panoramaSceneVo.setText(text);
            panoramaSceneVo.setImg(img);
            sceneVo.add(panoramaSceneVo);
        }

        Map<String, ValueVo> collect =
                houseShowScenes.stream().collect(Collectors.toMap(HouseShowScene::getHouseShowSceneId, HouseShowScene::getValueVo));

        panoramaVo.setScenes(sceneVo);
        ConfigVo configVo = new ConfigVo();
        configVo.setDefaultConfig(HouseShowConfig);
        configVo.setScenes(collect);
        panoramaVo.setConfigVo(configVo);
        return panoramaVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id) {
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }
}